#include "HyperLink.h"

CHyperLink::CHyperLink(const tstring &link)
	: m_link (link)
{
	setTextColor(RGB(0, 0, 255));
}

CHyperLink::~CHyperLink(void) {
}


void CHyperLink::onSetFont() {
	LOGFONT lf;
	::GetObject(m_font, sizeof(lf), &lf);
	lf.lfUnderline = TRUE;

	HFONT font = ::CreateFontIndirect(&lf);
	::DeleteObject(m_font);
	m_font = font;

	_CalcValidRect();
}

void CHyperLink::onSetText() {
	_CalcValidRect();
}

void CHyperLink::onSetAlign() {
	_CalcValidRect();
}

void CHyperLink::onSetIcon() {
	_CalcValidRect();
}

void CHyperLink::_CalcValidRect() {
	if (!IsWindow()) {
		return;
	}

	HDC hdc = GetDC();
	CDCHandle dc(hdc);

	CRect rcClient;
	GetClientRect(rcClient);

	HFONT oldFont = NULL;
	if (m_font) {
		dc.SelectFont(m_font);
	}

	CRect rc = rcClient;
	int height = dc.DrawText(m_title.c_str(), m_title.length(), rc, _GetDrawTextFormat() | DT_CALCRECT);
	
	if (m_icon) {
		rc.right += ICON_SIZE + ICON_PADDING; // enlarge the size
	}

	if (m_align == CENTER) {
		m_rcValid.left = (rcClient.Width() - rc.Width()) / 2;
		m_rcValid.right = m_rcValid.left + rc.Width();
	} else if (m_align == RIGHT) {
		m_rcValid.right = rcClient.right;
		m_rcValid.left = m_rcValid.right - rc.Width();
	} else {
		m_rcValid.left = rcClient.left;
		m_rcValid.right = m_rcValid.left + rc.Width();
	}
	m_rcValid.top = (rcClient.Height() - height) / 2;
	m_rcValid.bottom = m_rcValid.top + height;

	if (m_font) {
		dc.SelectFont(oldFont);
	}

	ReleaseDC(hdc);
}


void CHyperLink::setLink(const tstring &link) {
	m_link = link;
}

LRESULT CHyperLink::OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	int xPos = GET_X_LPARAM(lParam);
	int yPos = GET_Y_LPARAM(lParam);

	CPoint pnt(xPos, yPos);
	if (m_rcValid.PtInRect(pnt)) {
		HCURSOR cursor = ::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND));
		SetCursor(cursor);
	} else {
		bHandled = false;
	}
	return 0;
}

LRESULT CHyperLink::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	int xPos = GET_X_LPARAM(lParam);
	int yPos = GET_Y_LPARAM(lParam);

	CPoint pnt(xPos, yPos);
	if (m_rcValid.PtInRect(pnt)) {
		HCURSOR cursor = ::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND));
		SetCursor(cursor);

		::ShellExecute(NULL, _T("open"), m_link.c_str(), NULL, NULL, SW_SHOW);
	} else {
		bHandled = false;
	}
	return 0;
}

LRESULT CHyperLink::OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	int xPos = GET_X_LPARAM(lParam);
	int yPos = GET_Y_LPARAM(lParam);

	CPoint pnt(xPos, yPos);
	if (m_rcValid.PtInRect(pnt)) {
		HCURSOR cursor = ::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND));
		SetCursor(cursor);
	} else {
		bHandled = false;
	}
	return 0;
}

LRESULT CHyperLink::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	_CalcValidRect();
	bHandled = false;
	return 0;
}

LRESULT CHyperLink::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
	_CalcValidRect();
	bHandled = false;
	return 0;
}